Page({
  data: {
    isAuth: false,
    srcList: [], // 存储拍摄的图片路径
    devicePosition: "back",
    ka: false
  },
  onLoad() {
    // 获取用户授权信息
    const _this = this
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.camera']) {
          _this.setData({
            isAuth: true
          })
        } else {
          wx.authorize({
            scope: 'scope.camera',
            success() { 
              _this.setData({
                isAuth: true
              })
            },
            fail() {
              _this.openSetting().then(res => {
                _this.setData({
                  isAuth: true
                })
              })
            }
          })
        }
      },
      fail: res => {
        console.log('获取用户授权信息失败')
      }
    })
  },
  openSetting() {
    const _this = this
    let promise = new Promise((resolve, reject) => {
      wx.showModal({
        title: '授权',
        content: '请先授权获取摄像头权限',
        success(res) {
          if (res.confirm) {
            wx.openSetting({
              success(res) {
                if (res.authSetting['scope.camera']) { 
                  resolve(true)
                } else { 
                  _this.openSetting().then(res => {
                    resolve(true)
                  })
                }
              },
              fail(res) {
                console.log(res)
              }
            })
          } else if (res.cancel) {
            _this.openSetting().then(res => {
              resolve(true)
            })
          }
        }
      })
    })
    return promise;
  },
  // 拍照
  takePhoto() {
    this.setData({ ka: true })
    setTimeout(() => {
      this.setData({ ka: false })
    }, 200);

    // 创建相机上下文对象
    const ctx = wx.createCameraContext()
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        let tempImagePath = res.tempImagePath
        let srcList = this.data.srcList;
        srcList.push(tempImagePath); // 将拍摄的新图片路径添加到列表中
        this.setData({
          srcList: srcList
        })
      }
    })
  },
  // 切换摄像头
  change() {
    let position = this.data.devicePosition === 'front' ? "back" : 'front';
    this.setData({ devicePosition: position })
  },
  // 预览图片
  preview(event) {
    let currentSrc = event.currentTarget.dataset.src;
    wx.previewImage({
      current: currentSrc,
      urls: this.data.srcList
    })
  },

  complete() {
    const app = getApp(); // 获取全局应用实例
    app.globalData.photoList = this.data.srcList; // 将图片路径列表保存到全局变量中
    // 修改跳转路径，确保路径正确
    wx.navigateTo({
      url: '/pages/scan/edit/edit', // 确保这个路径是正确的
    });
  },

  // 选择相册图片
  chooseImage() {
    const _this = this;
    wx.chooseImage({
      count: 9 - _this.data.srcList.length, // 最多可以选择的图片张数，这里可以根据需要自行调整
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        let tempFilePaths = res.tempFilePaths;
        let srcList = _this.data.srcList.concat(tempFilePaths); // 将选择的图片路径添加到列表中
        _this.setData({
          srcList: srcList
        });
      }
    });
  }
})
